package com.mdyy.server.tcp.component;

public class UrlDecoder {

    // 01CC mcc：移动国家码  67-71
    // 01 mnc：移动网络码
    // 0760 lac：基站区域码
    // F9F6 cell id：基站编码
    // 16  rx：信号值
    // 0760 lac：基站区域码
    // ABD6  cell id：基站编码
    // 17  rx：信号值
    // 0760  lac：基站区域码
    // D2E6  cell id：基站编码
    // 0B  rx：信号值

    public static String getLBS(String raw) {

        int mcc = fromHex(raw.substring(66, 70));
        int mnc = fromHex(raw.substring(70, 72));

        int lac1 = fromHex(raw.substring(72, 76));
        int cell1 = fromHex(raw.substring(76, 80));
        int rx1 = fromHex(raw.substring(80, 82));

        int lac2 = fromHex(raw.substring(82, 86));
        int cell2 = fromHex(raw.substring(86, 90));
        int rx2 = fromHex(raw.substring(90, 92));

        int lac3 = fromHex(raw.substring(92, 96));
        int cell3 = fromHex(raw.substring(96, 100));
        int rx3 = fromHex(raw.substring(100, 102));

        String url = "http://apilocate.amap.com/position?accesstype=0&cdma=0&output=json&key=863a7418d5c2f1c41a0067511f383aec";

        StringBuilder builder = new StringBuilder(url);
        builder.append("&bts=")
                .append(mcc + ",")
                .append(mnc + ",")
                .append(lac1 + ",")
                .append(cell1 + ",")
                .append(rx1);

        builder.append("&nearbts=")
                .append(mcc + ",")
                .append(mnc + ",")
                .append(lac2 + ",")
                .append(cell2 + ",")
                .append(rx2)
                .append("|")
                .append(mcc + ",")
                .append(mnc + ",")
                .append(lac3 + ",")
                .append(cell3 + ",")
                .append(rx3);

        return builder.toString();
    }


    // 000000000000  MAC地址d0:76:e7:94:d0:3c
    // 00  信号值，0x3a=58-110=-52
    // 000000000000
    // 00
    // 000000000000
    // 00
    // A5
    public static String getWifi(String raw) {

        String mac1 = raw.substring(102, 114);
        int signal1 = fromHex(raw.substring(114, 116))-110;
        String mac2 = raw.substring(116, 128);
        int signal2 = fromHex(raw.substring(128, 130))-110;
        String mac3 = raw.substring(130, 142);
        int signal3 = fromHex(raw.substring(142, 144))-110;

        String url = "http://apilocate.amap.com/position?accesstype=1&output=json&key=863a7418d5c2f1c41a0067511f383aec";

        StringBuilder builder = new StringBuilder(url);
        builder.append("&macs=")
                .append(addComma(mac1) + ",")
                .append(signal1)
                .append("|")
                .append(addComma(mac2) + ",")
                .append(signal2)
                .append("|")
                .append(addComma(mac3) + ",")
                .append(signal3);

        return builder.toString();
    }


    public static int fromHex(String hex) {
        return Integer.parseInt(hex, 16);
    }

    //000000000000
    public static String addComma(String source){
        StringBuilder juejiang = new StringBuilder();
        char[] chars = source.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            juejiang.append(chars[i]);
            if(i%2==1){
                juejiang.append(":");
            }
        }
        return juejiang.substring(0, juejiang.length() - 1);
    }


    public static void main(String[] args) {
        String raw = "2480191555110531570612193802106953114265767C000000FFFFFBFF0000000001CC010760ABD61D0760F9F61B0760D2E610CC08FBECAFB84180B575BCED403680B575BCD90034CE";
        String urlLBS = UrlDecoder.getLBS(raw);
        String urlWifi = UrlDecoder.getWifi(raw);
        System.out.println(urlLBS);
        System.out.println(urlWifi);
    }

}

